/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkStateGridA/MkStateGridA.h"
#include <MkUtil/MkLog.h>
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include <signal.h>

CMkTimer     g_Timer;
MkString    g_AlarmSubId;
MkString    g_StateSubId;
MkString    VideoPhoneSession = "555";
MkString    RealplaySession = "666";
MkString    BroadcastSession = "777";

Uint32 MessageReqestCallback(CMkStateGridA *pSvr, const CMkStateGridARequestBase* pRequest, const MkStateGridAResponseCallback& ResCb)
{
    switch (pRequest->GetMsgType()) {
    case MkStateGridAMedia:
    {
        const CMkStateGridAMediaRequest* pReq = dynamic_cast<const CMkStateGridAMediaRequest*>(pRequest);
        if (pReq) {
            //realplay
            if ("34020000001110000001" == pReq->GetChannelId()) {
                CMkStateGridAMediaResponse Res(pReq->GetSessionId(), "127.0.0.1", 0,nullptr);
                RealplaySession = pReq->GetSessionId();
                ResCb(MkSipStatusOK, &Res);
            } else if ("34020000001210000001" == pReq->GetChannelId()) {
                //broadcast
                BroadcastSession = pReq->GetSessionId();
                CMkStateGridAMediaResponse Res(pReq->GetSessionId(), "127.0.0.1", 123,nullptr);
                ResCb(MkSipStatusOK, &Res);
            } else if ("34020000001310000001" == pReq->GetChannelId()) {
                VideoPhoneSession = pReq->GetSessionId();
                CMkStateGridAMediaResponse Res(pReq->GetSessionId(), "127.0.0.1", 123,nullptr);
                ResCb(MkSipStatusOK, &Res);
            } else {
                ResCb(MkSipStatusBadRequest, nullptr);
            }
        }
    }
    break;
    case MkStateGridAMediaShutDown:
        MkInfoLog("recv media shut down\n");
        ResCb(MkSipStatusOK, nullptr);
        break;
    case MkStateGridAPtzControl:
        MkInfoLog("recv ptz control\n");
        ResCb(MkSipStatusOK, nullptr);
        break;
    case MkStateGridASubscribe:
    {
        const CMkStateGridASubscribeRequest* pReq = dynamic_cast<const CMkStateGridASubscribeRequest*>(pRequest);
        g_AlarmSubId = pReq->GetSubscribeId();
    }
    ResCb(MkSipStatusOK, nullptr);
    MkInfoLog("recv alarm subscribe\n");
    pSvr->AlarmNotify(g_AlarmSubId, "State", "Code", "Name", 1, NowMkTime, NowMkTime, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
        MkDebugLog("query flow return error code:%d\n", ErrorCode);
    });
    break;
    default:
        break;
    }
    return NoneError;
}


int main(int argc, char ** argv)
{

#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;
    g_Timer.StartTimerThread();

    Pool.Start();
    CMkStateGridA LowSvr(&Pool, &g_Timer);
    CMkStateGridA* pSvr = &LowSvr;
    LowSvr.Start("34020000002000000001", "3402000000", "", 5070, [pSvr](const CMkStateGridARequestBase* pRequest, const MkStateGridAResponseCallback& ResCb) {
        return MessageReqestCallback(pSvr, pRequest, ResCb);
    });

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            g_Timer.LoopBreak();
        }
    });// set exit  signal
    char GetC;
    while (GetC = getchar()) {
        BOOL bQuit = FALSE;
        MkDebugLog("get char:%c\n", GetC);
        switch (GetC) {
        case '1':
            LowSvr.AlarmNotify(g_AlarmSubId, "State", "Code", "Name", 1, NowMkTime, NowMkTime, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case '2':
            LowSvr.StateNotify(g_StateSubId, "State", "Code", "Name", 1, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case '3':
            LowSvr.StopRealPlay(RealplaySession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case '4':
            //LowSvr.StopBrodcast(BroadcastSession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
            //    MkDebugLog("query flow return error code:%d\n", ErrorCode);
            //});
            break;
        case '5':
            //LowSvr.StopVideoPhone(VideoPhoneSession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase*) {
            //    MkDebugLog("query flow return error code:%d\n", ErrorCode);
            //});
            break;
        case 'q':
            bQuit = TRUE;
            break;
        default:
            break;
        }
        if (bQuit) {
            break;
        }
    }
    g_Timer.StopTimerThread();
    Pool.Stop();
    LowSvr.Stop();
#ifdef _WIN32
    WSACleanup();
#endif
    printf("end\n");
    return 0;
}


